from selenium.webdriver import Keys
from selenium.webdriver.support.select import Select

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
from selenium.common.exceptions import InvalidArgumentException, WebDriverException, NoSuchElementException
from autoframe.common.functions import open_browser


class Base:
    def __init__(self, driver):
        # 打开浏览器
        self.driver = driver
        time.sleep(1)

    def get(self, url):
        # 输入网址
        try:
            self.driver.get(url)
        except (InvalidArgumentException, WebDriverException):
            print('输入的网址有误')

    def find_element(self, locator, timeout=10):
        # 定位单个元素
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator), '定位失败')

        except NoSuchElementException:
            print('元素定位失败')

    def find_elements(self, locator, timeout=10):
        # 定位多个元素
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_all_elements_located(locator), '定位失败')
        except NoSuchElementException:
            print('元素定位失败')

    def get_attribute(self, locator, value):
        # 定位属性
        element = self.find_element(locator)
        if element:
            return element.get_attribute(value)

    def get_element_text(self, locator):
        # 获取元素文本内容
        element = self.find_element(locator)
        if element:
            return element.text

    def forward(self):
        # 前进
        self.driver.forward()

    def back(self):
        # 后退
        self.driver.back()

    def refresh(self):
        # 刷新
        self.driver.refresh()

    def click(self, locator):
        # 点击操作
        element = self.find_element(locator)
        if element:
            element.click()

    def quit(self, seconds=0):
        # 退出
        time.sleep(seconds)
        self.driver.quit()

    # TODO 下拉框选择
    def select_by_value(self, locator, value):
        element = self.find_element(locator)
        if element:
            s = Select(element)
            s.select_by_visible_text(value)

    # TODO 键盘操作
    def send_keys(self, locator, ipt):
        # 键盘输入内容
        element = self.find_element(locator)
        if element:
            element.send_keys(ipt)

    def send_keys_class(self, locator, clavier):
        element = self.find_element(locator)
        if clavier == '全选':
            element.send_keys(Keys.CONTROL, 'a')
        elif clavier == '复制':
            element.send_keys(Keys.CONTROL, 'c')
        elif clavier == '粘贴':
            element.send_keys(Keys.CONTROL, 'v')

    # TODO 获取 value
    def get_attribute_value(self, locator):
        element = self.find_element(locator)
        if element:
            return element.get_attribute('value')

    # TODO 弹窗处理
    def alert_accept(self):
        alter = self.driver.switch_to.alert
        alter.accept()  # 确定

    def alert_dismiss(self):
        alter = self.driver.switch_to.alert
        alter.dismiss()  # 取消

    # TODO 获取元素属性的TEXT
    def get_text(self, locator):
        value = self.find_element(locator)
        print(value.text)


if __name__ == '__main__':
    driver = open_browser()
    base = Base(driver)
    base.get('https://www.baidu.com')
    # base.get_attribute(By.ID, 'kw')
    base.forward()
    base.back()
    base.refresh()
